﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class Admin_AdminOrderDetails : System.Web.UI.Page
{
    private SqlConnection connect;
    private SqlDataAdapter da;
    private SqlCommand cmd;
    private DataSet ds;
    private string connectionString;
    int oid=0;
    string name = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        connectionString = WebConfigurationManager.ConnectionStrings["myCon"].ToString();
        connect = new SqlConnection(connectionString);
        connect.Open();
        ds = new DataSet();
        if (Request.QueryString["id"] != null)
        {
            oid = Int32.Parse(Request.QueryString["id"]);
            name = Request.QueryString["name"];
        }
        if(!IsPostBack)
        {
            //Label1.Text = Request.QueryString["a"];
            bindDataOrder(oid);
            bindDataOrderDetails(oid);
            bindDataCustomerDetails(name);
        }
    }

    protected void bindDataOrder(int id)
    {
        try
        {
            if (connect.State == ConnectionState.Closed)
            {
                connect.Open();
            }
            ds = new DataSet();
            cmd = new SqlCommand("spGetOrderDetails", connect);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@oid", SqlDbType.Int).Value = id;
            da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            DetailsView1.DataSource = ds.Tables[0];
            DetailsView1.DataBind();

        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            connect.Close();
        }
    }

    protected void bindDataCustomerDetails(string name)
    {
        try
        {
            if (connect.State == ConnectionState.Closed)
            {
                connect.Open();
            }
            ds = new DataSet();
            cmd = new SqlCommand("spGetCustomerDetails", connect);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = name;
            da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            DetailsView2.DataSource = ds.Tables[0];
            DetailsView2.DataBind();

        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            connect.Close();
        }
    }

    protected void updateOrder(int id, string status)
    {
        try
        {
            if (connect.State == ConnectionState.Closed)
            {
                connect.Open();
            }
            cmd = new SqlCommand("spUpdateOrder", connect);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@oid", SqlDbType.Int).Value = id;
            cmd.Parameters.Add("@status", SqlDbType.VarChar).Value = status;
            cmd.ExecuteNonQuery();

        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            connect.Close();
        }
    }

    protected void bindDataOrderDetails(int id)
    {
        try
        {
            if (connect.State == ConnectionState.Closed)
            {
                connect.Open();
            }
            ds = new DataSet();
            cmd = new SqlCommand("spGetOrderDetailsbyID", connect);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@oid", SqlDbType.Int).Value = id;
            da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();

        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            connect.Close();
        }
    }

    protected void btnShip_Click(object sender, EventArgs e)
    {
        updateOrder(oid,"Shipped");
        bindDataOrder(oid);
    }
    protected void btnDeclined_Click(object sender, EventArgs e)
    {
        updateOrder(oid, "Declined");
        bindDataOrder(oid);
    }
}